package com.wn.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wn.common.RedisConst;
import com.wn.model.entity.Book;
import com.wn.service.BookService;
import com.wn.mapper.BookMapper;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

/**
* @author KOO
* @description 针对表【t_book】的数据库操作Service实现
* @createDate 2024-12-08 14:53:59
*/
@Service
public class BookServiceImpl extends ServiceImpl<BookMapper, Book>
    implements BookService{

    /**
     * 按销量的降序排序，并且分页
     *
     * @param pageNum
     * @param pageSize
     */
    @Override
    @Cacheable(cacheNames = RedisConst.BOOK_LIST, keyGenerator = "keyGenerator")
    public Page<Book> findTopSales(Integer pageNum, Integer pageSize) {
        return baseMapper.selectPage(new Page<>(pageNum, pageSize),
                Wrappers.lambdaQuery(Book.class)
                        .orderByDesc(Book::getBuyCount)
        );
    }


    @Override
    @Cacheable(cacheNames = RedisConst.BOOK_LIST, keyGenerator = "keyGenerator")
    public Page<Book> findByType(Integer pageNum, Integer pageSize, Integer typeId) {
        return baseMapper.selectPage(new Page<>(pageNum, pageSize),
                Wrappers.lambdaQuery(Book.class)
                        .eq( Book::getTypeId, typeId)
                        .orderByDesc(Book::getUpdateTime)
        );
    }

    @Override
    @Cacheable(cacheNames = RedisConst.BOOK_DETAIL,key="#root.methodName + ':' + #id")
    public Book findById(Long id) {
        return baseMapper.selectById(id);
    }

}




